package com.dm.cloud.images.controller;

import cn.hutool.json.JSONObject;
import com.dm.cloud.core.mode.ResultModel;
import com.dm.cloud.images.common.mode.ImageMode;
import com.dm.cloud.images.common.mode.ImageTag;
import com.dm.cloud.images.mode.fileupload.FileUploadShardStartMode;
import com.dm.cloud.images.mode.fileupload.FileUploadShardStartResultMode;
import com.dm.cloud.images.mode.fileupload.ImageUploadMerge;
import com.dm.cloud.images.service.ImagesShardService;
import com.dm.cloud.images.service.RepositoryServerService;
import com.dm.cloud.oauthlib.service.anon.HasLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/apis/v1/imageServer")
@Tag(name =  "镜像管理")
@HasLogin
public class RepositoryServerController {


    @Autowired
    private RepositoryServerService imageServerService;

    @Autowired
    private ImagesShardService imagesShardService;

    @Operation(summary = "上传镜像")
    @PostMapping("/upImage")
    public ResultModel<String> upImage(@RequestParam("file") MultipartFile file){
        Object s=imageServerService.uploadImage(file);
        return ResultModel.success(s);
    }

    @PostMapping("upImageStart")
    @Operation(summary = "分片上传文件第一步：获取上传文件标识")
    public ResultModel<FileUploadShardStartResultMode> upImageStart(@RequestBody FileUploadShardStartMode fileUploadShardStartMode){
        FileUploadShardStartResultMode re=imagesShardService.shardStart(fileUploadShardStartMode);
        return ResultModel.success(re);
    }

    @PostMapping("imageShardUpload")
    @Operation(summary = "上传分片文件第二步：上传分片。 fileKey:第一个步骤返回的key, shardNum:当前分片编号 ")
    public ResultModel<Boolean> upImageShardUpload(MultipartFile file, String fileKey, Integer shardNum){
        Boolean isOk= imagesShardService.shardUpload(file,fileKey,shardNum);
//        if(isOk){
//            throw new CustomException("测试分片失败！");
//        }
        return ResultModel.success(isOk);
    }

    @PostMapping("imageShardMerge")
    @Operation(summary = "上传分片文件第三步：合并分片。并上传镜像，返回相对路径")
    public ResultModel<String> shardMerge(@RequestBody ImageUploadMerge fileUploadMerge){
        String filePath= imageServerService.imageShardMerge(fileUploadMerge);
        return ResultModel.success(filePath);
    }

    @Operation(summary = "查询镜像列表")
    @GetMapping("/searchImages")
    public ResultModel<List<ImageMode>> searchImages(
            @Parameter(description = "分页行数") Integer page_size,
            @Parameter(description = "页码，从1开始") Integer page,
            @Parameter(description = "排序：-creation_time,project_id  多个字段用逗号分割")String sort
            ,@Parameter(description = "条件:Query string to query resources. Supported query patterns are \"exact match(k=v)\", \"fuzzy match(k=~v)\", \"range(k=[min~max])\", \"list with union releationship(k={v1 v2 v3})\" and \"list with intersetion relationship(k=(v1 v2 v3))\". The value of range and list can be string(enclosed by \" or '), integer or time(in format \"2020-04-09 02:36:00\"). All of these query patterns should be put in the query string \"q=xxx\" and splitted by \",\". e.g. q=k1=v1,k2=~v2,k3=[min~max]") String q){
        List<ImageMode> images=imageServerService.searchImages(page,page_size,sort,q);
        return ResultModel.success(images);
    }

    @Operation(summary = "删除镜像")
    @DeleteMapping("deleteImage")
    public ResultModel<Boolean> deleteImage(@Parameter(in = ParameterIn.QUERY,description = "镜像名称，不带tag") String imageName){
        return ResultModel.success(imageServerService.deleteImage(imageName));
    }

    @Operation(summary = "列出镜像所有tag")
    @GetMapping("imageTags")
    public ResultModel<List<ImageTag>> imageTags(@Parameter(in = ParameterIn.QUERY,description = "镜像名称，不带tag") String imageName){
        return ResultModel.success(imageServerService.imageTags(imageName));
    }

    @Operation(summary = "查询镜像tag详情")
    @GetMapping("tagDetail")
    public ResultModel<JSONObject> tagDetail(
            @Parameter(in = ParameterIn.QUERY,description = "镜像名称，不带tag") String imageName,
            @Parameter(in = ParameterIn.QUERY,description = "tag的sha256Id") String sha256Ids
    ){
        return ResultModel.success(imageServerService.tagDetail(imageName,sha256Ids));
    }

    @Operation(summary = "删除tag")
    @DeleteMapping("deleteTag")
    public ResultModel<Boolean> deleteTag(
            @Parameter(in = ParameterIn.QUERY,description = "digest标识") String digest,
            @Parameter(in = ParameterIn.QUERY,description = "镜像名称") String imageName,
            @Parameter(in = ParameterIn.QUERY,description = "tag") String tag
            ){
        return ResultModel.success(imageServerService.deleteTag(digest,imageName,tag));
    }

}
